package com.ldxiwxr.common.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ldxiwxr.common.common.Medicine;
import com.ldxiwxr.common.common.MedicineHighLight;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.ldxiwxr.common.constant.MedicineApiConstant.*;


/**
 * 获取中药网api内容
 * @author SuBanxia
 */
public class MedicineApiUtils {

    /**
     * 获取中药网要闻
     * @param pageSize 文章条数
     * @return List<MedicineHighLight> 要闻list集合
     */
    public static List<MedicineHighLight> getMedicineHighlight(String pageSize) {

        //判断传入的值页数值是否为空，为空就默认为10
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }

        //发送GET请求获取获取返回的对应的json对象
        try {
            String rspStr = HttpUtil.createGet(MEDICINE_API_HTTP + ARTICLE_LIST_API + "?" + HIGHLIGHT_CID + "&pageSize=" +pageSize)
                    .execute()
                    .body();
            //判断返回对象是否为空
            if (StringUtils.isEmpty(rspStr)) {
                return null;
            }
            //读取获取到的数组内容
            JSONObject json = new JSONObject(rspStr);
            JSONArray list = json.getJSONArray("list");

            //新建高光list对象
            List<MedicineHighLight> medicineHighLightList = new ArrayList<>();
            MedicineHighLight medicineHighLight = null;

            //for循环赋值
            for (int i = 0; i < list.size(); i++) {
                //获取对应下标的内容
                JSONObject highlight = list.getJSONObject(i);
                //创建要闻对象
                medicineHighLight = new MedicineHighLight();
                //给要闻对象赋值
                medicineHighLight.setFileID(highlight.getStr("fileID"));
                medicineHighLight.setTitle(highlight.getStr("title"));
                medicineHighLight.setLeadTitle(highlight.getStr("leadTitle"));
                medicineHighLight.setArticleAbstract(highlight.getStr("abstract"));
                medicineHighLight.setPublishTime(highlight.getStr("publishTime"));
                medicineHighLight.setAuthor(highlight.getStr("author"));
                medicineHighLight.setSource(highlight.getStr("source"));
                //给list对象添加内容
                medicineHighLightList.add(medicineHighLight);
            }
            //返回结果
            return medicineHighLightList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //不执行成功,返回null
        return null;
    }

    /**
     * 根据文章id查询文章
     * @param articleId 文章id
     * @return 文章内容
     */
    public static MedicineHighLight getArticleContent(String articleId){
        //判断传入的文章id是否为空
        if (StringUtils.isBlank(articleId)) {
            return null;
        }

        //调用api获取文章内容
        try {
            String rspStr = HttpUtil.createGet(MEDICINE_API_HTTP + ARTICLE_API + "?aid=" + articleId)
                    .execute()
                    .body();

            //判断返回对象是否为空
            if (StringUtils.isEmpty(rspStr)) {
                return null;
            }
            MedicineHighLight medicineHighLight = new MedicineHighLight();
            //读取获取到的内容
            JSONObject json = new JSONObject(rspStr);
            medicineHighLight.setArticleContent(json.getStr("content"));
            medicineHighLight.setSource(json.getStr("source"));
            medicineHighLight.setArticleAbstract(json.getStr("abstract"));
            medicineHighLight.setAuthor(json.getStr("author"));
            medicineHighLight.setTitle(json.getStr("title"));
            medicineHighLight.setLeadTitle(json.getStr("source"));
            medicineHighLight.setPublishTime(json.getStr("publishTime"));

            return medicineHighLight;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取中药网中医文化
     * @param pageSize 文章条数
     * @return List<MedicineHighLight> 要闻list集合
     */
    public static List<MedicineHighLight> getCulture(String pageSize) {

        //判断传入的值页数值是否为空，为空就默认为10
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "6";
        }

        //发送GET请求获取获取返回的对应的json对象
        try {
            String rspStr = HttpUtil.createGet(MEDICINE_API_HTTP + ARTICLE_LIST_API + "?" + CULTURE_CID + "&pageSize=" +pageSize)
                    .execute()
                    .body();
            //判断返回对象是否为空
            if (StringUtils.isEmpty(rspStr)) {
                return null;
            }
            //读取获取到的数组内容
            JSONObject json = new JSONObject(rspStr);
            JSONArray list = json.getJSONArray("list");

            //新建高光list对象
            List<MedicineHighLight> medicineHighLightList = new ArrayList<>();
            MedicineHighLight medicineHighLight = null;

            //for循环赋值
            for (int i = 0; i < list.size(); i++) {
                //获取对应下标的内容
                JSONObject highlight = list.getJSONObject(i);
                //创建要闻对象
                medicineHighLight = new MedicineHighLight();
                //给要闻对象赋值
                medicineHighLight.setFileID(highlight.getStr("fileID"));
                medicineHighLight.setTitle(highlight.getStr("title"));
                medicineHighLight.setLeadTitle(highlight.getStr("leadTitle"));
                medicineHighLight.setArticleAbstract(highlight.getStr("abstract"));
                medicineHighLight.setPublishTime(highlight.getStr("publishTime"));
                medicineHighLight.setAuthor(highlight.getStr("author"));
                medicineHighLight.setSource(highlight.getStr("source"));
                //给list对象添加内容
                medicineHighLightList.add(medicineHighLight);
            }
            //返回结果
            return medicineHighLightList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //不执行成功,返回null
        return null;
    }

    /**
     * 根据关键词获取草药
     * @param word 关键词
     * @return 草药List集合
     */
    public static List<Medicine> getMedicineList(String word){
        //判断传入的草药关键值是否为空
        if (StringUtils.isBlank(word)) {
            return null;
        }

        //发送GET请求获取获取返回的对应的json对象
        try {
            String rspStr = HttpUtil.createGet(MEDICINE_QUERY_API + "?key=" + MEDICINE_QUERY_API_KEY + "&word=" + word + "&num=10")
                    .execute()
                    .body();
            //判断返回对象是否为空
            if (StringUtils.isEmpty(rspStr)) {
                return null;
            }
            //读取获取到的数组内容
            JSONObject json = new JSONObject(rspStr);
            JSONArray list = json.getJSONObject("result").getJSONArray("list");
            
            //新建草药list对象
            List<Medicine> medicineList = new ArrayList<>();
            Medicine medicine = null;

            //for循环赋值
            for (int i = 0; i < list.size(); i++) {
                //获取对应下标的内容
                JSONObject medicineContent = list.getJSONObject(i);
                //创建草药对象
                medicine = new Medicine();
                //给草药对象赋值
                medicine.setTitle(medicineContent.getStr("title"));
                medicine.setContent(medicineContent.getStr("content"));
                //给list对象添加内容
                medicineList.add(medicine);
            }
            //返回结果
            return medicineList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //不执行成功,返回null
        return null;
    }

    /**
     * 获取人物专访内容
     */
    public static MedicineHighLight getDoctor(){
        //发送GET请求获取获取返回的对应的json对象
        try {
            String rspStr = HttpUtil.createGet(MEDICINE_API_HTTP + ARTICLE_LIST_API + "?" + DOCTOR_CID + DOCTOR_SUFFIX )
                    .execute()
                    .body();
            //判断返回对象是否为空
            if (StringUtils.isEmpty(rspStr)) {
                return null;
            }
            //读取获取到的数组内容
            JSONObject json = new JSONObject(rspStr);
            JSONArray list = json.getJSONArray("list");

            //新建返回对象
            MedicineHighLight medicineHighLight = null;
            //for循环赋值
            for (int i = 0; i < list.size(); i++) {
                //获取对应下标的内容
                JSONObject highlight = list.getJSONObject(i);
                //创建要闻对象
                medicineHighLight = new MedicineHighLight();
                //给要闻对象赋值
                medicineHighLight.setFileID(highlight.getStr("fileID"));
                medicineHighLight.setTitle(highlight.getStr("title"));
                medicineHighLight.setLeadTitle(highlight.getStr("leadTitle"));
                medicineHighLight.setArticleAbstract(highlight.getStr("abstract"));
                medicineHighLight.setPublishTime(highlight.getStr("publishTime"));
                medicineHighLight.setAuthor(highlight.getStr("author"));
                medicineHighLight.setSource(highlight.getStr("source"));
                medicineHighLight.setPicture(highlight.getStr("pic1"));
            }
            //返回结果
            return medicineHighLight;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
